package com.hepinghospital.user.ui.activity.home;

import android.graphics.Canvas;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.adapter.DrugDirectoryAdapter;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.DrugResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import jz.joyoung.robot.util.SoftKeyboardUtil;

/**
 * @Description: 药品目录
 * @Author: lzp
 * @CreateDate: 2022/7/01 16:31
 * @UpdateRemark: 更新说明
 */
public class DrugDirectoryActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.edt_search)
    EditText edt_search;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    DrugDirectoryAdapter mAdapter;
    List<DrugResponse.Drug> drugs;
    String keyword = "";
    int page = 1;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_drug_directory;
    }

    @Override
    public void initData() {
        drugs = new ArrayList<>();
    }

    @Override
    protected void initViews() {
        tvTitle.setText("互联网药品目录");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        mAdapter = new DrugDirectoryAdapter(drugs);
        recyclerView.setAdapter(mAdapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }
        });
        edt_search.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                page = 1;
                keyword = edt_search.getText().toString();
                // 按下完成按钮，这里和上面imeOptions对应
                queryDrug(keyword);
                SoftKeyboardUtil.hideSoftKeyboard(DrugDirectoryActivity.this);
            }
            return false;
        });
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            queryDrug(keyword);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            queryDrug(keyword);
        });
        queryDrug(keyword);
    }

    public void queryDrug(String dragName) {
        RestProxy.getInstance().searchDrug(dragName, page, new BaseObserver<DrugResponse>() {
            @Override
            public void onSuccess(DrugResponse result) {
                if (page == 1) {
                    drugs.clear();
                }
                drugs.addAll(result.records);
                mAdapter.notifyDataSetChanged();
                if (drugs.isEmpty()) {
                    tvEmpty.setVisibility(View.VISIBLE);
                } else {
                    tvEmpty.setVisibility(View.GONE);
                }
                if (page == 1) {
                    refreshLayout.finishRefresh();
                } else {
                    if (result.records.isEmpty()) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                super.onError(e);
                if (page == 1) {
                    refreshLayout.finishRefresh();
                } else {
                    refreshLayout.finishLoadMore();
                }
            }
        });
    }
}
